package com.stsdai.order.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.stsdai.manager.mapper.OrderItemMapper;
import com.stsdai.manager.mapper.OrderMapper;
import com.stsdai.manager.mapper.OrderMapper2;
import com.stsdai.manager.mapper.OrderShippingMapper;
import com.stsdai.manager.pojo.Order;
import com.stsdai.manager.pojo.OrderItem;
import com.stsdai.manager.pojo.OrderShipping;
import com.stsdai.order.redis.RedisService;
import com.stsdai.order.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private OrderItemMapper orderItemMapper;
	
	@Autowired
	private OrderShippingMapper orderShippingMapper;
	
	@Autowired
	private RedisService redisService;
	
	@Resource
	private OrderMapper2 OrderMapper2;
	
	@Value("${STSD_ORDER_ID_KEY}")
	private String STSD_ORDER_ID_KEY;
	
	@Override
	public String saveOrder(Order order) {
		
		//保存咱们的Order
		//生成订单ID ： 生成订单策略： 自增长id + 用户ID (拼接)
		String orderId = "" + this.redisService.incr(this.STSD_ORDER_ID_KEY) + order.getUserId();
		
		order.setOrderId(orderId);
		//状态未付款 1
		order.setStatus(1);
		//订单创建时间
		order.setCreateTime(new Date());
		//订单的基本信息保存完了
		this.orderMapper.insertSelective(order);
		
		List<OrderItem> list = order.getOrderItems();
		for (OrderItem orderItem : list) {
			orderItem.setOrderId(orderId);
			//保存订单商品信息
			this.orderItemMapper.insertSelective(orderItem);
		}
		
		//保存订单物流信息
		OrderShipping shipping = order.getOrderShipping();
		shipping.setOrderId(orderId);
		this.orderShippingMapper.insertSelective(shipping);
		
		return orderId;
	}

	@Override
	public Order queryOrderById(String id) {
		
		//根据订单ID来查询订单信息
		Order order = this.orderMapper.selectByPrimaryKey(id);
		
		OrderItem orderItem = new OrderItem();
		orderItem.setOrderId(id);
		//再获取Order中的商品集合
		List<OrderItem> orderItemList = this.orderItemMapper.select(orderItem);
		order.setOrderItems(orderItemList);
		
		OrderShipping orderShipping = new OrderShipping();
		orderShipping.setOrderId(id);
		//获取订单物流
		OrderShipping shipping = this.orderShippingMapper.selectOne(orderShipping);
		order.setOrderShipping(shipping);
		
		return order;
	}

	@Override
	public void cleanOrder() {
		//根据什么条件来清理?
		//1、在线支付
		//2、订单状态为未付款状态 status = 1
		//3、订单创建时间为两天前
		Example example = new Example(Order.class);
		example.createCriteria()
			.andEqualTo("status", 1)
			.andLessThan("createTime", new DateTime().minusDays(2).toDate());
		//来把订单状态修改为： 6、交易关闭；close_time 时间
		//参数说明：1、修改后的Order对象是什么吗样子的！2、要修改的条件
		Order order = new Order();
		order.setStatus(6);
		order.setCloseTime(new Date());
		this.orderMapper.updateByExampleSelective(order, example);
	}

	@Override
	public Order queryNewOrder(Long userId) {
		Order order = OrderMapper2.queryNewOrder(userId);
		return order;
	}

	@Override
	public void updateOrder(Order order) {
		//修改订单
		orderMapper.updateByPrimaryKey(order);
		//修改订单中的商品信息
		List<OrderItem> list = order.getOrderItems();
		for (OrderItem orderItem : list) {
			//修改订单商品信息
			this.orderItemMapper.updateByPrimaryKey(orderItem);
		}
		
	}

}
